package com.slf.engine.threads;

import java.io.UnsupportedEncodingException;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import com.slf.engine.common.SysContants;
import com.slf.engine.common.SysContext;
import com.slf.engine.exception.EngineException;
import com.slf.engine.http.HttpResponseWrapper;
import com.slf.engine.services.IBjdxService;
import com.slf.engine.services.impl.BjdxServiceImpl;
import com.slf.engine.utils.ConnectUtil;

public class HeartThread extends Thread {

	private static final Logger log = Logger.getLogger(HeartThread.class);
	public HeartThread()
	{
		log.log(Priority.INFO, "HEART_THREAD RUN SUCCESS");
	}
	
	public void run()
	{
		if(SysContext.FREE_TIME == 0)
		{
			SysContext.FREE_TIME = System.currentTimeMillis();
		}
		while(true)
		{
			if(System.currentTimeMillis() - SysContext.FREE_TIME > 1000*3*60)
			{
				try {
					SysContext.FREE_TIME = System.currentTimeMillis();
					log.log(Priority.INFO, "开始执行心跳访问");
					HttpResponseWrapper wrapper = ConnectUtil.getInStantic().execute("GET", "http://219.141.157.23/vams/header.jsp", SysContants.DEFAULT_CHARSET, null, null, null, null, null);
					if(wrapper.getBody() != null)
					{
						try {
							String body = new String(wrapper.getBody(),SysContants.DEFAULT_CHARSET);
							if(body.indexOf("无权访问") != -1)
							{
								IBjdxService bjdxService = new BjdxServiceImpl();
								bjdxService.doLogin();
							}
						} catch (UnsupportedEncodingException e) {
							// TODO Auto-generated catch block
						}
					}
				} catch (EngineException e) {
					// TODO Auto-generated catch block
				}
			}else{
				try {
					Thread.sleep(60*1000);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
				}
			}
		}
	}
}
